package com.zaofan.utils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.zaofan.bean.Account;
import com.zaofan.bean.User;
import com.zaofan.bean.t_address;
import com.zaofan.service.AccountService;

public class DataUtils {

	public static void main(String[] args) {
		String sql = "select * from ecs_users";
		AccountService accountService = new AccountService();
		try {
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
			List<User> users = runner.query(sql, new BeanListHandler<>(User.class));
			System.err.println(users.size());
			Account account = null;
			for (User user : users) {
				account = new Account();
				account.setMobile(user.getUser_name());
				account.setNickname(user.getUser_name());
				account.setPhoto(user.getUser_photo());
				account.setMoney(user.getUser_money());
				account.setBirthday(user.getBirthday());
				account.setRegeistDate(new Date(user.getReg_time()));
				account.setLastLoginTime(new Date(user.getLast_login()));
				account.setPassword(WebUtils.MD5(user.getQuestion()));
				account.setEmailIsActive("y");
				account.setFreeze("n");
				if (user.getSex() == 0) {
					account.setSex("保密");
				} else if (user.getSex() == 1) {
					account.setSex("男");
				} else if (user.getSex() == 2) {
					account.setSex("女");
				}
				accountService.add(account);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static List<t_address> getUserAddress(int userId) {
		String sql = "select * from ecs_user_address where user_id = " + userId;
		try {
			System.err.println(sql);
			QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource());
			List<t_address> users = runner.query(sql, new BeanListHandler<>(t_address.class));
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}